/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.hq.mes.bussiness.inrecorddetail.service.impl;

import com.hq.mes.bussiness.inrecorddetail.domain.InrecordDetail;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.hq.mes.bussiness.inrecorddetail.repository.InrecordDetailRepository;
import com.hq.mes.bussiness.inrecorddetail.service.InrecordDetailService;
import com.hq.mes.bussiness.inrecorddetail.service.dto.InrecordDetailDto;
import com.hq.mes.bussiness.inrecorddetail.service.dto.InrecordDetailQueryCriteria;
import com.hq.mes.bussiness.inrecorddetail.service.mapstruct.InrecordDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wsq
* @date 2021-12-24
**/
@Service
@RequiredArgsConstructor
public class InrecordDetailServiceImpl implements InrecordDetailService {

    private final InrecordDetailRepository inrecordDetailRepository;
    private final InrecordDetailMapper inrecordDetailMapper;

    @Override
    public Map<String,Object> queryAll(InrecordDetailQueryCriteria criteria, Pageable pageable){
        Page<InrecordDetail> page = inrecordDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inrecordDetailMapper::toDto));
    }

    @Override
    public List<InrecordDetailDto> queryAll(InrecordDetailQueryCriteria criteria){
        return inrecordDetailMapper.toDto(inrecordDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InrecordDetailDto findById(Long id) {
        InrecordDetail inrecordDetail = inrecordDetailRepository.findById(id).orElseGet(InrecordDetail::new);
        ValidationUtil.isNull(inrecordDetail.getId(),"InrecordDetail","id",id);
        return inrecordDetailMapper.toDto(inrecordDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InrecordDetailDto create(InrecordDetailDto resources) {
        if(inrecordDetailRepository.findByCode(resources.getCode()) != null){
            throw new EntityExistException(InrecordDetail.class,"code",resources.getCode());
        }
        return inrecordDetailMapper.toDto(inrecordDetailRepository.save( inrecordDetailMapper.toEntity(resources)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InrecordDetailDto resources) {
        InrecordDetail inrecordDetail = inrecordDetailRepository.findById(resources.getId()).orElseGet(InrecordDetail::new);
        ValidationUtil.isNull( inrecordDetail.getId(),"InrecordDetail","id",resources.getId());
        InrecordDetail inrecordDetail1 = null;
        inrecordDetail1 = inrecordDetailRepository.findByCode(resources.getCode());
        if(inrecordDetail1 != null && !inrecordDetail1.getId().equals(inrecordDetail.getId())){
            throw new EntityExistException(InrecordDetail.class,"code",resources.getCode());
        }
        inrecordDetail.copy( inrecordDetailMapper.toEntity(resources));
        inrecordDetailRepository.save(inrecordDetail);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            inrecordDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InrecordDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InrecordDetailDto inrecordDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编码", inrecordDetail.getCode());
            map.put("名称", inrecordDetail.getName());
            map.put("数量", inrecordDetail.getQuantity());
            map.put("单位", inrecordDetail.getUnit());
            map.put("备注", inrecordDetail.getRemark());
            map.put("id", inrecordDetail.getInrecordId());
            map.put("id", inrecordDetail.getProductId());
            map.put("创建人", inrecordDetail.getCreateBy());
            map.put("创建时间", inrecordDetail.getCreateTime());
            map.put("最后修改人", inrecordDetail.getUpdateBy());
            map.put("最后修改时间", inrecordDetail.getUpdateTime());
            map.put("是否已删除", inrecordDetail.getIsDel());
            map.put("产品编码", inrecordDetail.getProductCode());
            map.put("产品名称", inrecordDetail.getProductName());
            map.put("产品规格", inrecordDetail.getProductSpecification());
            map.put(" version",  inrecordDetail.getVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}